// Author: InfiniteStarlight
#include<bits/stdc++.h>
using namespace std;
#define sz(v) (int)((v).size())
#define ALL(v) (v).begin(),(v).end()
#define rALL(v) (v).rbegin(),(v).rend()
#define srt(v) sort(ALL(v))
#define rsrt(v) sort(rALL(v))
#define lb(v,x) (int)(lower_bound(ALL(v),x)-(v).begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-(v).begin())
#define uni(v) (v).resize(unique(ALL(v))-(v).begin())
#define rev(v) reverse(ALL(v))
#define mp make_pair
#define pb emplace_back
#define inf 0x3f3f3f3f
using pii=pair<int,int>;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
using i128=__int128_t;
using u128=__uint128_t;
#define ls (x<<1)
#define rs (ls|1)
const int maxn=(1<<17)+5;
int n,m,n2;
int ra[maxn],a[maxn],c[maxn];
int nd[maxn],lay[maxn],sr[maxn];
string s[20];
int x[4];
string S;
ll val[20][maxn];
int tot;
int seq[maxn];
void get(int x,int l,int r)
{
	if(l==r)
	{
		seq[tot++]=l;
		return ;
	}
	int mid=(l+r)/2;
	if(S[x]&1)
	{
		get(rs,mid+1,r);
		get(ls,l,mid);
	}
	else
	{
		get(ls,l,mid);
		get(rs,mid+1,r);
	}
}
int main()
{
	freopen("arena.in","r",stdin);
	freopen("arena.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=0;i<n;i++)
		cin>>ra[i];
	for(int i=1;i<=m;i++)
	{
		cin>>c[i];
		c[i]--;
	}
	int k=0;
	while((1<<k)<n) k++;
	n2=1<<k;
	for(int i=0;i<k;i++)
		cin>>s[i];
	S="?";
	for(int i=k-1;i>=0;i--)
		S+=s[i];
	for(int i=1;i<=m;i++)
	{
		nd[i]=1;
		lay[i]=k;
		int r=n2-1;
		while((r>>1)>=c[i])
		{
			nd[i]<<=1;
			lay[i]--;
			r>>=1;
			if(!r) break;
		}
		sr[i]=r;
	}
	int t;
	cin>>t;
	while(t--)
	{
		for(int i=0;i<4;i++)
			cin>>x[i];
		for(int i=0;i<n;i++)
			a[i]=ra[i]^x[(i+1)&3];
		int nd=1;
		int r=n2-1;
		int v1[22],v2[22],tmp2[22];
		for(int i=k;i;i--)
		{
			tot=0;
			get(nd,0,r);
			for(int j=0;j<tot;j++)
				val[i][j]=0;
			for(int j=0;j<tot;j++)
			{
				for(int x=1;x<=i;x++)
					if(!(j>>(x-1)&1))
					{
						v1[x]=-1;
						v2[x]=inf;
					}
					else break;
				int p=(!j)?0:__lg(j)+1;
				int pos=inf;
				int x2=i;
				while(x2&&(j>>(x2-1)&1)) x2--;
				if(a[seq[j]]<x2) pos=min(pos,seq[j]);
				for(int x=1;x<=p;x++)
					pos=min(pos,v2[x]);
				// cerr<<i<<" "<<j<<" "<<seq[j]<<" "<<pos<<endl;
				// cerr<<v1[1]<<" "<<v1[2]<<" "<<v1[3]<<endl;
				// cerr<<v2[1]<<" "<<v2[2]<<" "<<v2[3]<<endl;
				pos=min(pos,tot);
				if(pos>0)
					val[i][pos-1]+=seq[j]+1;
				int Mx=-1;
				int pp=0;
				for(int x=1;x<=i;x++)
				{
					if(!(j>>(x-1)&1)) pp=x;
					int tmp=Mx;
					if(a[seq[j]]<pp) tmp=inf;
					else tmp=max(tmp,seq[j]);
					tmp2[x]=min(v2[x],tmp);
					Mx=max(Mx,v1[x]);
				}
				int Mn=inf;
				pp=0;
				for(int x=1;x<=i;x++)
				{
					if(!(j>>(x-1)&1)) pp=x;
					int tmp=Mn;
					if(a[seq[j]]<pp) tmp=min(tmp,seq[j]);
					v1[x]=max(v1[x],tmp);
					Mn=min(Mn,v2[x]);
				}
				for(int x=1;x<=i;x++)
					v2[x]=tmp2[x];
			}
			for(int j=tot-1;j;j--)
				val[i][j-1]+=val[i][j];
			nd<<=1;
			r>>=1;
		}
		ll ans=0;
		for(int i=1;i<=m;i++)
			if(!lay[i])
			{
				ans^=i;
				// cerr<<1<<endl;
			}
			else
			{
				ans^=val[lay[i]][c[i]]*i;
				// cerr<<val[lay[i]][c[i]]<<endl;
			}
		cout<<ans<<'\n';
	}
	return 0;
}